import axios from 'axios';
import moment from 'moment-timezone';
import {BaseUrl} from '~/utils/common/envType';
import {downloadBlob} from '~/utils/common/download';
import http from './http';
import constant from './constant';

const instance = axios.create({
    // timeout: 3000,
    retry: 3,
    retryDelay: 1000,
    withCredentials: true,
    responseType: 'blob'
});

/**
 * 获取部门选择列表
 */
const getDept = () => http.post(constant.SALES.DEPARTMENT);

/**
 * 渠道列表
 */
const getChannel = deptName => http.post(constant.SALES.CHANNEL, {name: deptName});

/**
 * 投放填报查询
 * @param {Object} params 搜索参数
 */
const advSearch = params => http.post(constant.SALES.ADV_LIST, params);

/**
 * 群明细查询
 * @param {Object} params
 */
const chatRoomSearch = params => http.post(constant.SALES.CHATROOM_LIST, params);

const deleteAdv = params => http.post(constant.SALES.DELETE, params);

/**
 * 创建投放
 * @param {Object} params
 */
const createAdv = params => http.post(constant.SALES.CREATE, params);
const updateAdv = params => http.post(constant.SALES.UPDATE, params);

const advDownload = (params, name = '文件') => {
    const today = moment().format('YYYYMMDDHHmmss');
    const promise = new Promise((resolve, reject) => {
        instance
            .post(`${BaseUrl}${constant.SALES.ADV_DOWNLOAD}`, params)
            .then(res => {
                // application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型
                const blob = new Blob([res.data], {
                    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
                });
                downloadBlob(blob, `${name}_${today}.xlsx`);
            })
            .catch(error => {
                reject(error);
            });
    });
    return promise;
};

const chatRoomDownload = (params, name = '文件') => {
    const today = moment().format('YYYYMMDDHHmmss');
    const promise = new Promise((resolve, reject) => {
        instance
            .post(`${BaseUrl}${constant.SALES.CHATROOM_DOWNLOAD}`, params)
            .then(res => {
                // application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型
                const blob = new Blob([res.data], {
                    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
                });
                downloadBlob(blob, `${name}_${today}.xlsx`);
            })
            .catch(error => {
                reject(error);
            });
    });
    return promise;
};

/**
 * 更新销售名称
 * @param {Object}} params
 */
const updateName = params => http.post(constant.SALES.UPDATE_NAME, params);

/**
 * 查询销售投放的详情
 * @param {Object} params {id}
 */
const advDetail = params => http.post(constant.SALES.DETAIL, params);

/**
 * 群类别获取
 * @param {Object} params
 */
const getCategory = params => http.post(constant.SALES.CATEGORY, params);

export {
    getDept,
    advDownload,
    getChannel,
    deleteAdv,
    advSearch,
    chatRoomSearch,
    updateName,
    chatRoomDownload,
    createAdv,
    updateAdv,
    advDetail,
    getCategory
};
